<!--  -->
<script setup>
import useReportInfo from './composable/useReportInfo'
import { ref } from 'vue'
import Pagination from '@/components/Pagination.vue'
import SubmitBtn from '@/components/button/SubmitBtn.vue'
import { format } from '@/utils/time'
import Tip from '@/components/tip'
import RejectWindow from './component/RejectWindow.vue'
import { rejectReport } from '@/apis/report'
import ResolveWindow from './component/ResolveWindow.vue'
import { useBreadsStore } from '@/stores'
import { RouterLink } from 'vue-router'

const { setBreadCrumbs } = useBreadsStore()
setBreadCrumbs([{ title: '审查' }])

const loading = ref(false)
const pageSize = 2
const currPage = ref(1)
const { reportList, total, getPageData } = useReportInfo(currPage.value, pageSize)
const rejectWindowVisible = ref(false)
const resolveWindowVisible = ref(false)
const currHandleReport = ref(null)

function retrieveNextPage(v) {
  loading.value = true
  currPage.value = v
  getPageData(v, pageSize)
  loading.value = false
}

function handleRejectReport(v) {
  rejectReport(currHandleReport.value.id, v.reason.join(','), v.info)
    .then(() => {
      Tip.success('操作成功')
      rejectWindowVisible.value = false
      retrieveNextPage(1)
    })
}

</script>

<template>
  <div class="view-container">
    <ResolveWindow :info="currHandleReport?.userInfo.username" :condition="resolveWindowVisible"
      @close="resolveWindowVisible = false"></ResolveWindow>
    <RejectWindow :info="currHandleReport?.userInfo.username" :condition="rejectWindowVisible"
      @close="rejectWindowVisible = false" @submit="handleRejectReport"></RejectWindow>
    <div class="left-wrapper"></div>
    <div class="right-wrapper">
      <div>
        <div class="report-container" v-for="(v, i) in reportList" :key="v.id">
          <div class="header">
            <span>#{{ i + 1 + (currPage - 1) * pageSize }}</span>
            <span>类型：{{ v.objectType }}</span>
            <span>{{ v.userInfo.username }} 举报 {{ v.reportedUserInfo.username }}</span>
            <span>{{ v.status }}</span>
            <RouterLink :to="`/${v.objectType}/${v.objectId}`">链接</RouterLink>
            <div style="margin-left:auto">创建于{{ format(v.createAt, 'YYYY-MM-DD HH:mm:ss') }}</div>
          </div>
          <div class="body">
            <p>举报原因：{{ v.flaggedContent }}</p>
            <p>附加消息：{{ v.extraInfo }}</p>
            <p>内容：{{ v.content }}</p>
          </div>
          <div class="footer">
            <div class="left">
              <div class="user-card" v-for="user in [v.userInfo, v.reportedUserInfo]" :key="user.id">
                <img :src="user.avatar" class="avatar" />
                <div class="user-info">
                  <span>{{ user.displayName || '-' }}</span>
                  <RouterLink :to="`/users/${user.username}`">@{{ user.username }}</RouterLink>
                </div>
                <div class="user-info">
                  <span>声望：{{ user.ranks }}</span>
                  <span>角色：{{ user.roles.join('.') }}</span>
                </div>
              </div>
            </div>
            <div class="right">
              <SubmitBtn @click="rejectWindowVisible = true; currHandleReport = v">驳回</SubmitBtn>
              <SubmitBtn @click="resolveWindowVisible = true; currHandleReport = v">处理</SubmitBtn>
            </div>
          </div>
        </div>
      </div>
      <div class="pagination-container">
        <Pagination position="center" v-model:page="currPage" :numPages="5" :showPages="pageSize" :total="total"
          :oneShow="true" :isTotal="true" :is-start-end="true" @pageChange="retrieveNextPage"></Pagination>
      </div>
    </div>

  </div>
</template>

<style scoped lang="scss">
.view-container {
  flex: 1;
  background-color: var(--view-bgc-01);
  display: flex;

  .left-wrapper {
    border-right: 1px dashed var(--border-secondary);
    padding-right: 1em;
    width: 20%;
  }

  .right-wrapper {
    padding: 0 1em;
    flex: 1;
    display: flex;
    flex-direction: column;
  }
}

.report-container {
  margin: 10px 0;
  width: 100%;
  padding: 20px;
  border-radius: 4px;
  background-color: var(--view-bgc-02);
  box-shadow: 0px 0px 6px rgba(54, 83, 56, 0.297);

  a:hover {
    color: red;
  }

  .header {
    border-bottom: 1px solid var(--border-secondary);
    padding-bottom: 1em;
    display: flex;

    span {
      margin-right: 20px;
    }
  }

  .body {
    margin: 1em 0;
  }

  .footer {
    border-top: 1px solid var(--border-secondary);
    padding-top: 1em;
    display: flex;
    justify-content: space-between;
    align-items: end;

    .left {
      min-width: 50%;
      display: flex;
      justify-content: space-between;
      gap: 30px;

      .user-card {
        display: flex;

        .avatar {
          width: 40px;
          height: 40px;
          border-radius: 4px;
          background-color: var(--bgc-02);
          margin-right: 10px;
          margin-right: 5px;
        }

        .user-info {
          display: flex;
          margin-right: 1em;
          flex-direction: column;
        }

      }
    }

    .right {
      display: flex;
      gap: 10px;
    }

  }
}

.pagination-container {
  margin-top: auto;
  width: 100%;
  display: flex;
  justify-content: end;
  align-items: center;
}
</style>
